﻿using System;
using System.Collections.Generic;
using System.Text;
using Common;
using System.Reflection;
using System.Collections;

namespace LoginServer
{
    public partial class ManagerMachine
    {
        byte machine_id;
        MachineConnection connection;
        LoginServer loginServer;
        List<int> tables;
        List<int> rooms=new List<int>();
        CONFIG config;
        public ManagerMachine(CONFIG config,byte ID,LoginServer loginServer, ManagerRoom managerroom)
        {
            this.machine_id = ID;
            this.loginServer = loginServer;
            this.config = config;
            InitConnection();
            tables = new List<int>();
            foreach (DictionaryEntry k in managerroom.Rooms())
            {
                Room tmp = (Room)k.Value;
                if (ID == tmp.info.ServerId)
                {
                    int id = tmp.info.RoomID;
                    rooms.Add(id);
                }
            }
        }
        void InitConnection()
        {
            connection = new MachineConnection(config.IPSERVER, config.MACHINES[machine_id].PORT_SERVER, config.MACHINES[machine_id].IP_MACHINE);
            connection.Machine_ID = machine_id;
            connection.MachineReceive += new MachineConnection.MachineReceiveHandle(connection_MachineReceive);
            connection.MachineDisconnect += new EventHandler<MachineDisconnectEventArgs>(connection_MachineDisconnect);
        }
               
        public void Start()
        {
            connection.Start();
        }
        public byte Manager_ID
        {
            get { return machine_id; }
        }      
        public List<int> List_Table
        {
            get { return tables; }
        }
        public int CountTable
        {
            get { return tables.Count; }
        }
        public void AddTable(int TableID)
        {
            tables.Add(TableID);
        }


        internal void RemoveTable(int tableid)
        {
            tables.Remove(tableid);
        }

        public List<int> List_Room
        {
            get { return rooms; }
        }
        public int CountRoom
        {
            get { return rooms.Count; }
        }
        public void AddRoom(int RoomID)
        {
            rooms.Add(RoomID);
        }

        internal void RemoveRoom(int RoomJoinID)
        {
            rooms.Remove(RoomJoinID);
        }
        public bool CheckRoomExist(int RoomID)
        {
            for (int i = 0; i < rooms.Count; i++)
            {
                if (rooms[i] == RoomID)
                    return true;
            }
            return false;
        }
        public bool CheckExist(int TableID)
        {
            for (int i = 0; i < tables.Count;i++ )
            {
                if (tables[i] == TableID)
                    return true;
            }
            return false;
        }

        public void SendMessage(MSG_GSM message, params object[] param)
        {
            connection.SendMessage(message, param);
        }
        public void SendMessage(MSG_GSM message, byte[] param)
        {
            connection.SendMessage(message, param);
        }

        void connection_MachineReceive(object sender, MachineReceiveEventArgs args)
        {
            MSG_GSM message = (MSG_GSM)args.Data[0];           
            //Console.WriteLine("Receive: " + message.ToString());
            List<object> param = new List<object>();
            param.Add(args.user);
            for (int i = 1; i < args.Data.Length; i++)
            {
                param.Add((byte)args.Data[i]);
            }
            try
            {
                Type t = typeof(ManagerMachine);
                MethodInfo handler = t.GetMethod("Process_" + message.ToString(), BindingFlags.NonPublic | BindingFlags.Instance);
                handler.Invoke(this, new object[] { param.ToArray() });
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("Error MSG_GSM." + message.ToString());
            }     
        }

        void connection_MachineDisconnect(object sender, MachineDisconnectEventArgs e)
        {
            Console.WriteLine("machine at port = " + e.machine.portServer + " disconnected");
            connection = null;
            InitConnection();
            Start();
        }

      
       
    }
}
